﻿class SWFLoader extends egret.DisplayObjectContainer {
    private data: any;
    private texture: egret.Texture;
    public constructor() {
        super();
    }

    private key: string;

    public initData(_key: string): void {
        this.key = _key;
        var loader: egret.URLLoader = new egret.URLLoader();
        loader.dataFormat = egret.URLLoaderDataFormat.TEXT;
        loader.addEventListener(egret.Event.COMPLETE, this.onLoadJsonComplete, this);
        var url: string = QNService.getFullUrl(this.key);
        var request: egret.URLRequest = new egret.URLRequest(url);
        loader.load(request);
    }

    private onLoadJsonComplete(event: egret.Event): void {
        var loader: egret.URLLoader = <egret.URLLoader>event.target;
        this.data = JSON.parse(loader.data);

        var loader: egret.URLLoader = new egret.URLLoader();
        loader.dataFormat = egret.URLLoaderDataFormat.TEXTURE;
        loader.addEventListener(egret.Event.COMPLETE, this.onLoadTextureComplete, this);
        var url: string = QNService.getFullUrl(this.key.replace(".json",".png"));
            //"resource/assets/mc/mctest.png";
        var request: egret.URLRequest = new egret.URLRequest(url);
        loader.load(request);
    }

    private onLoadTextureComplete(event: egret.Event): void {
        var loader: egret.URLLoader = <egret.URLLoader>event.target;
        this.texture = <egret.Texture>loader.data;

        this.createMovieClip();
    }

    private createMovieClip(): void {
        //创建动画工厂

        var mc_name: string;
        for (var s in this.data.mc)
        {
            mc_name = s;
        }
        var mcDataFactory: egret.MovieClipDataFactory = new egret.MovieClipDataFactory(this.data, this.texture);
        //创建 MovieClip，将工厂生成的 MovieClipData 传入参数
        this.mc = new egret.MovieClip(mcDataFactory.generateMovieClipData(mc_name));
        this.addChild(this.mc);
        //添加播放完成事件
        this.mc.addEventListener(egret.Event.COMPLETE, function () {
            console.log("COMPLETE");
        }, this);
        //添加循环播放完成事件
        this.mc.addEventListener(egret.Event.LOOP_COMPLETE, function () {
            console.log("LOOP_COMPLETE");
        }, this);
        //播放攻击动画

        this.mc.touchEnabled = true;
        this.mc.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.startDrag, this)
        this.mc.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.dragging, this)
        this.mc.addEventListener(egret.TouchEvent.TOUCH_END, this.stopDrag, this)
        this.mc.gotoAndStop(1);
    }

    private mc: egret.MovieClip;

    private draggind: boolean;
    private sx: number;
    private sy: number;

    private startDrag(event: egret.TouchEvent): void {
        this.draggind = true;
        this. sx = event.localX;
        this.sy = event.localY;

        this.mc.gotoAndPlay(this.mc.currentFrame, -1);
    }

    private dragging(event: egret.TouchEvent): void {
        if (this.draggind) {
            var mx = event.localX - this.sx;
            var my = event.localY - this.sy;

            this.mc.x += mx;
            this.mc.y += my;

            this.sx = event.localX;
            this.sy = event.localY;
        }
    }

    private stopDrag(event: egret.TouchEvent): void {
        this.draggind = false;
        this.mc.gotoAndPlay(this.mc.currentFrame,1);
    }
} 